<?php
    class auth extends CI_Controller {

        function __construct() {
            parent::__construct();           
            $this->load->model("auth_model");
			$this->load->model('logs_model');
        }
    
        function login() {
        	$result = $this->auth_model->check_user($this->input->post('meno'), $this->input->post('heslo'));    
            if( $result ) {
            //if( strcmp($this->input->post('heslo'), "autobus") == 0 ) {
            	$sessionData = array(
            		'user_id' => $result['id'],
            		'meno' => $result['meno'],
            		'admin' => $result['admin'],
            		'logged_in' => TRUE
				);
				
				$logsAction = 'prihlasil:' . $result['id'] . ':' . $result['meno'] . ':' . $result['admin']; //vytvorim action co urobil user
				
				$this->session->set_userdata($sessionData);
				
				$this->logs_model->addLog($this->session->userdata('user_id'), $logsAction); //pridam do logs data a userId
				
				redirect('SluzobneCesty');
            } else {
            	//chyba pri prihlaseni zle heslo alebo meno
            	redirect('SluzobneCesty');
            }
        }
        
        function logout() {
        	$logsAction = 'odhlasil:' . $this->session->userdata('user_id') . ':' . $this->session->userdata('meno') . ':' . $this->session->userdata('admin'); //vytvorim action co urobil user 
        	$this->logs_model->addLog($this->session->userdata('user_id'), $logsAction); //pridam do logs data a userId
            $this->session->unset_userdata(array('user_id'=>'', 'meno'=>'', 'admin'=>'', 'logged_in' => FALSE));
			
			redirect('SluzobneCesty');
        }

        
    }
    
?>
